import {
  api,
  util,
  storage
} from "../util";

import {
  deepMerge,
  isEmpty
} from '../newUtil'

import authorize from './authorize.js'

const app = getApp()

/**
 * 打印数据
 * 
 * 示例代码
 * ```js
 * logInfo('获取商品列表', req, options)
 * logInfo('获取结果', res)
 * ```
 */
function logInfo() {
  let [title, ...others] = arguments
  console.groupCollapsed(`====== ${title} ======`)
  for (let i in others) {
    console.log(others[i])
  }
  console.groupEnd()
}

const customer = {
  /**
   * 获取客户列表
   * @param {*} options 
   */
  getCustomerList(options = {}) {
    let entity = authorize.getEntity()
    if (!entity) {
      entity = authorize.getEntity('supplier')
    }
    console.log('entity', entity)
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      PageIndex: options.pageIndex || 1,
      PageType: options.pageType || 0, // 0 开单 1客户列表 7 公海客户
      onlyArrears: false,
      PageSize: options.pageSize || 20,
      FKId: entity ? entity.fkId : 0,
      Condition: `<lngs>${options.longitude || 0}</lngs>
      <lats>${options.latitude || 0}</lats>
      <Passed>${isEmpty(options.passed) ? 1 : options.passed}</Passed>
      <Keyword>${options.searchKey || options.keyword || ''}</Keyword>
      <LevelId>${options.levelId || ''}</LevelId>
      <TypeId>${options.typeId || ''}</TypeId>
      <NoOrderDay>${options.noOrderDay || ''}</NoOrderDay>
      <NoFollowDay>${options.noFollowDay || ''}</NoFollowDay>
      <ArrearsAmount>${options.arrearsAmount || ''}</ArrearsAmount>
      <TagId>${options.tagId || ''}</TagId>
      <WXTagIds>${options.wxTagIds || ''}</WXTagIds>
      <Status Oper="!=">-1</Status>`,
      OrderBy: options.orderBy || `<AddTime>DESC</AddTime><Id>DESC</Id>`,
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
    }
    let consoleTitle = '获取客户列表'
    logInfo(`${consoleTitle} 开始`, params, entity)
    return new Promise((resolve, reject) => {
      util.post('supplier.store.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      })
    })
  },
  /**
   * 添加/修改用户
   * @param {*} options 
   */
  addCustomer(options = {}) {
    let entity = authorize.getEntity()

    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      qr: '', // TODO: 未知参数 2_1166_2374
    }
    params = deepMerge(options, params)
    let consoleTitle = '添加/修改客户'
    logInfo(`${consoleTitle} 开始`, params, entity)
    return new Promise((resolve, reject) => {
      util.post('supplier.shop.save', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      })
    })
  },
  /**
   * 获取客户标签
   * @param {*} options 
   */
  getCustomerTagList(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      FKId: app.dns.proprietorId,
      FKFlag: app.dns.proprietor,
      UserId: entity ? entity.fkId : 0,
      parentId: 0
    }
    let consoleTitle = '获取客户标签'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('clerk.custom.wxtag', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户类型
   * @param {*} options 
   */
  getCustomerType(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      ProprietorId: app.dns.proprietorId
    }
    let consoleTitle = '获取客户类型'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('store.type.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户等级
   * @param {*} options 
   */
  getCustomerLevel(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      ProprietorId: app.dns.proprietorId
    }
    let consoleTitle = '获取客户等级列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('store.level.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户警告等级
   * @param {*} options 
   */
  getCustomerWarning(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      FKId: app.dns.proprietorId,
      FKFlag: app.dns.proprietor,
      UserId: 0,
      parentId: 0
    }
    let consoleTitle = '获取客户警告等级'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('clerk.custom.tag', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户自定义信息
   * @param {*} options 
   */
  getCustomerCustomInfo(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    }
    let consoleTitle = '获取客户自定义信息'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('store.customer.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户信息
   * @param {*} options 
   */
  getCustomerInfo(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      storeId: options.id || ''
    }
    let consoleTitle = '获取客户信息'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('store.shop.getinfo', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户时间信息
   * @param {*} options 
   */
  getCustomerTime(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      storeId: options.id || ''
    }
    let consoleTitle = '获取客户时间信息'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('store.order.latetime', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.1')
    })
  },
  /**
   * 获取客户统计数据
   * @param {*} options 
   */
  getCustomerCount(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      FKId: options.id || '',
      FKFlag: 1,
      QueryDate: options.date || '',
      storeId: options.id || ''
    }
    let consoleTitle = '获取客户统计数据'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('supplier.order.customerdetailnumber', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.1')
    })
  },
  /**
   * 获取客户信息（部分）
   * @param {*} options 
   */
  getCustomerHead(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      StoreId: options.id || ''
    }
    let consoleTitle = '获取客户信息（部分）'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.head.info', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户详情信息
   * @param {*} options 
   */
  getCustomerDetail(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      StoreId: options.id || ''
    }
    let consoleTitle = '获取客户详情信息'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.details.info', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取线索/任务的来源/销售阶段信息
   * @param {*} options 
   */
  getClueTypeList(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      Type: options.type || 'SaleStage',
    }
    let consoleTitle = '获取线索/任务的来源/销售阶段信息'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.type.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取任务/线索统计图表数据
   * @param {*} options 
   */
  getClueCount(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      ManagerId: entity && entity.manager ? entity.manager.ManagerId : '',
      Type: options.type || 'Clue',
      State: options.state || -1,
    }
    let consoleTitle = '获取任务/线索统计图表数据'
    logInfo(`${consoleTitle} 开始`, params, entity)
    return new Promise((resolve, reject) => {
      util.post('oa.task.getsalestagetotal', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取任务/线索统计数据列表
   * @param {*} options 
   */
  getClueCountList(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      StoManagerId: entity && entity.manager ? entity.manager.ManagerId : '',
      Type: options.type || 'Clue',
    }
    let consoleTitle = '获取任务/线索统计数据列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.getcluestatisticsdetails', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取任务/线索列表
   * @param {*} options 
   */
  getClueList(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      KeyWord: options.keyword || '',
      Type: options.type || 'Clue',
      State: options.state || -1,
      SourceId: '',
      SaleStageId: '',
      pageIndex: options.pageIndex || 1,
      pageSize: options.pageSize || 20,
      ManagerId: entity && entity.manager ? entity.manager.ManagerId : '',
      Repetitive: options.repetitive ? 1 : 0,
      sortField: options.sortField || '',
      sortDirect: options.sortDirect || ''
    }
    let consoleTitle = '获取任务/线索列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取员工列表
   * @param {*} options 
   */
  getStaffList(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      Type: options.type || 'Manager',
      Keyword: options.keyword || '',
      PageSize: options.pageSize || 100000,
      PageIndex: options.pageIndex || 1,
      FilterUser: options.filterUser || 0,
    }
    let consoleTitle = '获取员工列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('supplier.manager.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },

  /**
   * 添加 任务/线索
   * @param {*} options 
   */
  addClue(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      content: `<IsDelete>0</IsDelete>
      <ProprietorId>${app.dns.proprietorId || ''}</ProprietorId>
      <Proprietor>${app.dns.proprietor || ''}</Proprietor>
      <FKId>${app.dns.proprietorId || ''}</FKId>
      <FKFlag>${app.dns.proprietor || ''}</FKFlag>`
    }
    params.content = options.content + params.content
    let consoleTitle = '添加 任务/线索'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.add', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 修改 任务/线索
   * @param {*} options 
   */
  editClue(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      content: `<IsDelete>0</IsDelete>
      <ProprietorId>${app.dns.proprietorId || ''}</ProprietorId>
      <Proprietor>${app.dns.proprietor || ''}</Proprietor>
      <FKId>${app.dns.proprietorId || ''}</FKId>
      <FKFlag>${app.dns.proprietor || ''}</FKFlag>`
    }
    params.content = options.content + params.content
    let consoleTitle = '修改 任务/线索'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.update', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 修改线索负责人
   * @param {*} options 
   */
  editClueManager (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      content: options.content || ''
    }
    let consoleTitle = '修改线索负责人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.update', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取线索详情
   */
  getClueDetail(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      TaskId: options.id || '',
    }
    let consoleTitle = '获取线索详情'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.details', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 线索成交
   */
  clueComplete(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      TaskId: options.id || '',
    }
    let consoleTitle = '线索成交'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.done', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 放弃线索
   */
  clueCancel(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      TaskId: options.id || '',
    }
    let consoleTitle = '放弃线索'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.cancel', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取员工详情
   * @param {*} options 
   */
  getStaffDetail(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      ManagerId: options.id || ''
    }
    let consoleTitle = '获取员工详情'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('clerk.details.userinfo', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  
  getProductList(options = {}) {
    let entity = authorize.getEntity()
    let params = {}
    let consoleTitle = '获取列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('store.level.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      })
    })
  },
  getClueTagList(options = {}) {
    let entity = authorize.getEntity()
    let params = {}
    let consoleTitle = '获取列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('store.level.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      })
    })
  },
  /**
   * 获取已购商品
   * @param {*} options 
   */
  getPurchasedProductList(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      StoreId: options.id || '',
      KeyWord: options.searchKey || '',
      PageIndex: options.pageIndex || 1,
      PageSize: options.pageSize || 20,
    }
    let consoleTitle = '获取已购商品'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.deal.goods', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取售后和服务类型
   * @param {*} options 
   */
  getAfterSaleType(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
    }
    let consoleTitle = '获取售后和服务类型'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.load.followtype', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户历史订单记录
   * @param {*} options 
   */
  getHistoryOrderList(options = {}) {
    let entity = authorize.getEntity()
    console.log(entity)
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      PageIndex: options.pageIndex || 1,
      PageSize: options.pageSize || 20,
      ToFKId: app.dns.proprietorId,
      ToFKFlag: app.dns.proprietor,
      Status: -1,
      SearchKeyword: options.searchKey || '',
      SortField: 'a.Id',
      SortDirect: 'DESC',
      FKId: options.id || '',
      FromFKFlag: 1,
      FromFKId: options.id || '',
      StartOrderAddTime: options.startTime || '',
      EndOrderAddTime: options.endTime || '',
    }
    let consoleTitle = '获取客户历史订单记录'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('order.order.listbybuyers', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      })
    })
  },
  /**
   * 获取客户全部历史订单记录（包括线下）
   * @param {*} options 
   */
  getAllHistoryOrderList(options = {}) {
    let entity = authorize.getEntity()
    console.log(entity)
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      PageIndex: options.pageIndex || 1,
      PageSize: options.pageSize || 20,
      KeyWord: options.searchKey || '',
      StoreId: options.id || '',
      DateType: options.dateType || '',
      QueryDate: options.queryDate || '',
    }
    let consoleTitle = '获取客户全部历史订单记录（包括线下）'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('supplier.erp.salesorder', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.1')
    })
  },
  /**
   * 获取客户续费订单列表
   * @param {*} options 
   */
  getRenewalOrderList(options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      PageIndex: options.pageIndex || 1,
      pagesize: options.pageSize || 20,
      UserId: entity ? entity.fkId : 0,
      KeyWord: options.keyword || '',
      Type: options.type || '',
      Flag: 1
    }
    let consoleTitle = '获取客户续费订单列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('order.aftersale.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.1')
    })
  },
  /**
   * 获取线索跟进记录
   * @param {*} options 
   */
  getClueFollowList (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      TaskId: options.id || ''
    }
    let consoleTitle = '获取线索跟进记录'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.followrecord.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 添加线索跟进记录
   * @param {*} options 
   */
  addClueFollow (options = {}) {
    let entity = authorize.getEntity() || {}
    let role = entity.role || {}
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      TaskId: options.id || '',
      Image: options.img || '',
      Content: options.content || '',
      NextTime: options.nextTime || '',
      SaleStage: options.saleStage || '',
      Creator: role.Name || '',
      CreatorId: role.Id || ''
    }
    let consoleTitle = '添加线索跟进记录'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.followrecord.add', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取线索联系人列表
   * @param {*} options 
   */
  getClueContactList (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      TaskId: options.id || '',
      pageIndex: options.pageIndex || 1,
      pageSize: options.pageSize || 500,
    }
    let consoleTitle = '获取线索联系人列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.linkman.list', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },

  /**
   * 获取线索联系人
   * @param {*} options 
   */
  getClueContactDetail (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      LinkManId: options.id || ''
    }
    let consoleTitle = '获取线索联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.linkman.details', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },

  /**
   * 添加线索联系人
   * @param {*} options 
   */
  addClueContact (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    }
    params = deepMerge(params, options)
    let consoleTitle = '添加线索联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.linkman.add', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },

  /**
   * 修改线索联系人
   * @param {*} options 
   */
  editClueContact (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    }
    params = deepMerge(params, options)
    let consoleTitle = '修改线索联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.linkman.update', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 删除线索联系人
   * @param {*} options 
   */
  deleteClueContact (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      LinkManId: options.id || ''
    }
    let consoleTitle = '删除线索联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.linkman.del', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  
  /**
   * 获取线索负责人变更记录
   * @param {*} options 
   */
  getClueManagerChangedList (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      TaskId: options.id || '',
      pageIndex: options.pageIndex || 1,
      pageSize: options.pageSize || 500,
    }
    let consoleTitle = '获取线索负责人变更记录'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('oa.task.salesmanchangelog', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 添加售后记录/跟进记录
   * @param {*} options 
   */
  addFollowRecord (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      StoreId: options.id || '',
      UserId: options.userId || 0,
      ManagerId: entity && entity.manager ? entity.manager.ManagerId : '',
      FollowType: options.followType || '',
      Image: options.img || '',
      Content: options.content || ''
    }
    let consoleTitle = '添加售后记录/跟进记录'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.add.followrecordforstore', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户联系人列表
   * @param {*} options 
   */
  getContactList (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      StoreId: options.id || '',
      pageIndex: options.pageIndex || 1,
      pageSize: options.pageSize || 20,
    }
    let consoleTitle = '获取客户联系人列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.link.man', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取客户跟进联系人
   * @param {*} options 
   */
  getFollowContact (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      StoreId: options.id || ''
    }
    let consoleTitle = '获取客户跟进联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.load.followcontact', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 添加联系人
   * @param {*} options 
   */
  addContact (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    }
    params = deepMerge(params, options)
    let consoleTitle = '添加联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.add.linkman', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },

  /**
   * 获取联系人详情
   * @param {*} options 
   */
  getContactDetail (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      Id: options.id || ''
    }
    let consoleTitle = '获取联系人详情'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.load.linkmandetail', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },

  /**
   * 修改联系人
   * @param {*} options 
   */
  editContact (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    }
    params = deepMerge(params, options)
    let consoleTitle = '修改联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.update.linkman', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 删除联系人
   * @param {*} options 
   */
  deleteContact (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      Id: options.id || ''
    }
    let consoleTitle = '删除联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.del.linkman', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 获取微信联系人列表
   * @param {*} options 
   */
  getWxContactList (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      KeyWord: options.searchKey || '',
    }
    let consoleTitle = '获取微信联系人列表'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.select.wxuser', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 微信联系人设置为联系人
   * @param {*} options 
   */
  wxToContact (options = {}) {
    let entity = authorize.getEntity()
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      StoreId: options.id || '',
      wxuserid: options.userId || ''
    }
    let consoleTitle = '微信联系人设置为联系人'
    logInfo(`${consoleTitle} 开始`, params)
    return new Promise((resolve, reject) => {
      util.post('scrm.bind.storemanager', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.0')
    })
  },
  /**
   * 添加客户拜访记录
   * @param {*} options 
   */
  addCustomerVisit(options = {}) {
    let entity = authorize.getEntity()
    if (!entity) {
      entity = authorize.getEntity('supplier')
    }
    let params = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      ClientId: options.customer,
      ClientFlag: 1,
      Longitude: options.longitude || '',
      Latitude: options.latitude || '',
      Province: options.province || '',
      City: options.city || '',
      Area: options.area || options.district || '',
      Address: options.address || '',
      Content: options.content || '',
      Picture: options.img || '',
      DeviceSN: 'WechatMiniProgram',
    }
    let consoleTitle = '添加客户拜访记录'
    logInfo(`${consoleTitle} 开始`, params, entity)
    return new Promise((resolve, reject) => {
      util.post('supplier.signbook.add', params, function (res) {
        logInfo(`${consoleTitle} 结束`, res)
        res = res.data
        resolve(res)
      }, '3.1')
    })
  },
}

export {
  customer
}